<?php

namespace Inscripcion\Model\Dao;

/**
 * Componentes necesarios para el modelado
 */
use Zend\Db\Adapter\Adapter;
use Zend\Db\Sql\Sql;
use Inscripcion\Model\Entity\MateriaCursar;

/**
 * @package Dao
 */
class MateriaCursarDAO {

    protected $dbAdapter;

    /**
     * Nombre de la Tabla
     * @var type 
     */
    protected $tabla = 'materiacursar';
    private static $materiaCursarDao;

    /**
     * Configura Adaptador de Base de Datos
     * 
     * @param \Zend\Db\Adapter\Adapter $adapter
     */
    private function __construct(Adapter $adapter) {
        $this->dbAdapter = $adapter;
    }

    /**
     * 
     * @param type $adapter
     * @return instancia de MateriaCursarDao
     * Patron singleton
     * PROBADO
     */
    public static function getInstance($adapter) {
        if (self::$materiaCursarDao == null) {
            self::$materiaCursarDao = new MateriaCursarDao($adapter);
        }
        return self::$materiaCursarDao;
    }

    //no tiene id unico (estructura vieja)
    public function getMateriasCursar($id) {
        $sql = new Sql($this->dbAdapter);
        $select = $sql->select();
        $select->from($this->tabla);
        $select->where("salumid='" . $id . "'");
        $selectString = $sql->getSqlStringForSqlObject($select);
        $adapter = $this->dbAdapter;
        $results = $adapter->query($selectString, $adapter::QUERY_MODE_EXECUTE);
        ;
        return $results;
    }

    /**
     * Realiza una consulta utilizandon left join
     * @param type $cedula: La cedula del estudiante
     * @return type
     * SQL SELECT "materiacursar".* FROM "materiacursar" WHERE salumid='@param'
     * PROBADO
     */
    public function getMateriaCursarsSegunEstudiante($cedula) {
        $sql = new Sql($this->dbAdapter);
        $select = $sql->select();
        $select->from($this->tabla);
        $select->where("salumid='" . $cedula . "'");
        $selectString = $sql->getSqlStringForSqlObject($select);
        $adapter = $this->dbAdapter;
        $results = $adapter->query($selectString, $adapter::QUERY_MODE_EXECUTE);
        return $results;
        
    }
    
    /*public function getMateriaCursarSegunIdMalla($id,$malla){
        $sql = new Sql($this->dbAdapter);
        $select = $this ->select();
        //$this ->select()->setIntegrityCheck(false);
	$select->from(array('mt' => 'materia_cursar'), array('id_pensum'));
	$select->joinInner(array('m' => 'materia'), 'mt.id_pensum = m.codigo_externo' , array(id));
	$select->where("mt.id ='". $id . "'");
        $selectString = $sql->getSqlStringForSqlObject($select);
        $adapter = $this->dbAdapter;
        $results = $adapter->query($selectString, $adapter::QUERY_MODE_EXECUTE);
        return $results;
    }

    /**
     * @param type $data
     * @return type
     */
//    public function createRegistro($data){
//        $columns = array();
//        $values = array();
//        foreach($data as $column=>$value){
//            $columns[]  = $column;
//            $values[$column]   = (string)$value;
//        }
//        $sql = new Sql($this->dbAdapter);
//        $insert = $sql->insert();
//        $insert->into($this->tabla);
//        $insert->columns($columns);
//        $insert->values($values);
//        $statement = $sql->prepareStatementForSqlObject($insert);
//        if($statement->execute()){
//            return true;
//        }else{
//            return false;
//        }
//    }

    /**
     * @param type $id
     * @param type $data
     * @return type
     */
//    public function updateRegistro(MateriaCursar $materiaCursar){
////        $a = "salumestado";
//        $materiaCursarArray = $materiaCursar->objectToArray($materiaCursar);
////        print_r($materiaCursarArray);
//        $sql = new Sql($this->dbAdapter);
//        $update = $sql->update();
//        $update->table($this->tabla);
//        $update->set($materiaCursarArray);
//        $update->where("salumid='".$materiaCursar->getId()."'");
//        $statement = $sql->prepareStatementForSqlObject($update);
////        if($statement->execute()){
////            return true;
////        }else{
//            return false;
////        }
//    }

    /**
     * Eliminar un registro en base al id
     * @param type $id
     * @return bool
     */
//    public function deleteRegistro($id) {
//        $sql = new Sql($this->dbAdapter);
//        $delete = $sql->delete();
//        $delete->from($this->tabla);
//        $delete->where('salumid='.$id);
//        $statement = $sql->prepareStatementForSqlObject($delete);
//        if($statement->execute()){
//            return true;
//        }else{
//            return false;
//        }
//    }
}
